package Server.controller;

import Plug.result.RetResponse;
import Plug.result.RetResult;
import Server.dao.DataResourcesDao;
import Server.dao.DictionarysDao;
import Server.entity.DataResources;
import Server.entity.Diction;
import Server.entity.Users;
import Server.service.CommonService;
import net.sf.json.JSONArray;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ResourcesController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private DataResourcesDao dataResourcesDao;
    @Autowired
    private DictionarysDao dictionarysDao;
    @Autowired
    private CommonService commonService;
    /*
     *
     * 后台
     *
     * */
    @RequestMapping("home")
    public String home(){
        if(dictionarysDao.findByKeyst("BIND").getVal().equals("NO")){
            return "redirect:login?is=no";
        }
        return "home";
    }

    /*
     *
     * 登录
     *
     * */
    @RequestMapping("login")
    public String login(){
        return "login";
    }

    /*
     *
     * 页面资源访问
     *
     * */
    @RequestMapping("/vsp/{path}")
    public String vsp(@PathVariable String path){
        DataResources record = dataResourcesDao.findByUuid(path);
        return record.getUri();
    }
    /*
     *
     * 表头资源访问
     *
     * */
    @RequestMapping("/bsp/{path}")
    @ResponseBody
    public RetResult dsp(@PathVariable String path){
        DataResources record = dataResourcesDao.findByUuid(path);
        Diction fspView = new Diction();
        if(record!=null){
            fspView=dictionarysDao.findByKeyst(record.getUri());
        }
        return RetResponse.makeOKRsp(JSONArray.fromObject(fspView.getVal()));
    }

    /*
     *
     * 获得导航
     *
     * */

    @RequestMapping("/CommonController/GetNavigation")
    @ResponseBody
    public RetResult GetNavigation(){
        Users fspuser = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        return RetResponse.makeOKRsp(commonService.GetNavigation(fspuser));
    }

}
